package h0909;
/**
  * @description 移动0
  * @author 不知名帅哥
  * @date 2024/9/9 20:52
  * @version 1.0
*/
public class MoveZero {
    public static void main(String[] args) {

    }

    public void moveZeroes(int[] nums) {
        int k=0;
        for (int num : nums) {
            if (num!=0){
                nums[k]=num;
            }
        }
        while (k< nums.length){
            //都赋值为0
            nums[k++]=0;
        }
    }
    public void moveZeroes2(int[] nums) {
        int i=0;
        for (int j = 0; j < nums.length; j++) {
            if (nums[j]!=0){
                swap(nums,i,j);
                i++;
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }
}
